/**
 * related to myListcellTemplate.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-05
 */
var nf = sm("do_Notification");
var do_Button_rest = ui("do_Button_rest");
var do_Button_stop = ui("do_Button_stop");
var do_Button_edit = ui("do_Button_edit");
var do_Button_order = ui("do_Button_order");
var page = sm("do_Page");
var root = ui("$");//$是这个ui文件根节点组件的通配符，如果指定组件的id，也可以用id来获取对象
var common = require("common");

var userUtil = require("helper/user");
var urlUser = require("url/user");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");

root.setMapping({
	"do_Button_rest.tag" : "id",
	"do_Button_stop.tag" : "status",
	"do_Button_edit.tag" : "typeId"
});

root.on("dataRefreshed", function(){
	var status = do_Button_stop.tag;
	var typeId = do_Button_edit.tag;
	if (status == 1) {
		do_Button_stop.text = "暂停接单";
	} else if (status == 2) {
		do_Button_stop.text = "开启接单";
	}
});

//设置休息时间
do_Button_rest.on("touch",function(){
	common.open("source://view/homemaking/manage/restTime.ui",{id:do_Button_rest.tag,typeId:do_Button_edit.tag});
})

//开启or暂停服务
do_Button_stop.on("touch",function(){
	var status = do_Button_stop.tag;
	if (status == 1) {
		qqHttp.post(homemakingUtil.url.base.close, {id:do_Button_rest.tag}, function(data) {
			nf.toast("已暂停接单");
			do_Button_stop.text = "开启接单";
			do_Button_stop.tag = 2;
		});
	} else if (status == 2) {
		qqHttp.post(homemakingUtil.url.base.open, {id:do_Button_rest.tag}, function(data) {
			nf.toast("已开启接单");
			do_Button_stop.text = "暂停接单";
			do_Button_stop.tag = 1;
		});
	}
})

//编辑
do_Button_edit.on("touch",function(){
	if (userUtil.hasIdentityAuth()==="2") { //未认证的话
		qqHttp.post(urlUser.url.base.detail, {}, function(data) {
			if (data.userBase.hasIdentityAuth == 2) {
				nf.toast("请先完成身份认证再操作");
				return;
			} else if (data.userBase.hasIdentityAuth == 1) { //已认证，则更新
				userUtil.hasIdentityAuth("1");
				common.open("source://view/homemaking/manage/edit.ui",{id:do_Button_rest.tag});
			}
		});
	} else {
		common.open("source://view/homemaking/manage/edit.ui",{id:do_Button_rest.tag});
	}
})

//预约
do_Button_order.on("touch",function(){
	common.open("source://view/homemaking/order/orderList.ui",do_Button_rest.tag);
})